package com.manage.project.controller;


import com.manage.common.Result;
import com.manage.common.entity.FileInfo;
import com.manage.project.bean.vo.UpYunMakeDirInfo;
import com.manage.project.service.UpYunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


@Api(value = "又拍云", tags = {"又拍云"})
@RestController
@RequestMapping("ypy")
@CrossOrigin
public class UpYunController {

    @Resource
    private UpYunService upYunService;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////文件夹操作

    @PostMapping("/mkDir")
    @ApiOperation(value = "目录创建", notes = "创建目录")
    public Result<Boolean> mkdir(@ApiParam("目录路径名") @Validated UpYunMakeDirInfo filePath) {
        return Result.success(upYunService.createDir(filePath.getPath()));
    }

    @DeleteMapping("/rmDir")
    @ApiOperation(value = "目录删除", notes = "删除目录，若目录下有文件则删除失败返回false")
    public Result<Boolean> rmdir(@ApiParam("目录路径名") @Validated UpYunMakeDirInfo filePath) {
        return Result.success(upYunService.deleteDir(filePath.getPath()));
    }


    @PostMapping("/readDir")
    @ApiOperation(value = "目录地址读取", notes = "读取目录地址")
    public Result<String> readDirIter(@ApiParam("目录路径名") @Validated UpYunMakeDirInfo filePath) {
        return Result.success(upYunService.readDirIter(filePath.getPath()));
    }


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////文件操作

    @PostMapping("/cover/upload/multipartFile")
    @ApiOperation(value = "文件覆盖上传", notes = "若空间内指定目录已存在相同文件，则会被覆盖，且不可逆。")
    public Result<String> coverUploadByMultipartFile(MultipartFile file, String filePath) {
        return Result.success(upYunService.coverUploadMultipartFile(file, filePath));
    }

    @PostMapping("/upload/multipartFile")
    @ApiOperation(value = "文件非覆盖上传", notes = "若空间内指定目录已存在相同文件，不会覆盖，会提示。")
    public Result<String> uploadByMultipartFile(MultipartFile file, String filePath) {
        return Result.success(upYunService.uploadMultipartFile(file, filePath));
    }

    @PostMapping("/movefile")
    @ApiOperation(value = "文件移动", notes = "同 bucket 下移动文件，可以进行文件重命名、文件移动。它只能操作文件，不能操作文件夹。")
    public Result<Boolean> movefile(@ApiParam("目标路径") String path, @ApiParam("资源路径") String resourcePath, @ApiParam String bucketName) {
        return Result.success(upYunService.movefile(path, resourcePath, bucketName));
    }

    @PostMapping("/copyfile")
    @ApiOperation(value = "文件复制", notes = "同 bucket 下复制文件。它只能操作文件，不能操作文件夹。")
    public Result<Boolean> copyfile(@ApiParam("目标路径") String path, @ApiParam("资源路径") String resourcePath, @ApiParam("桶名") String bucketName) {
        return Result.success(upYunService.copyfile(path, resourcePath, bucketName));
    }

    @PostMapping("/deleteFile")
    @ApiOperation(value = "文件删除", notes = "根据资源位置删除文件")
    public Result<Boolean> deleteFile(@ApiParam("目录路径名") @Validated UpYunMakeDirInfo filePath) {
        return Result.success(upYunService.deleteFile(filePath.getPath()));
    }

    @PostMapping("/getFileInfo")
    @ApiOperation(value = "文件信息获取", notes = "根据文件存储路径，获取文件信息")
    public Result<FileInfo> getFileInfo(@ApiParam("目录路径名") @Validated UpYunMakeDirInfo filePath) {
        return Result.success(upYunService.getFileInfo(filePath.getPath()));
    }

}
